// const BASE_URL = "https://test.xlshhr.com/beauty-industry";
const BASE_URL = "https://www.xlshhr.com/beauty-industry";

class Request {
    /**
     * Request请求方法
     * @param  {String} url    链接
     * @param  {Objece} params 参数
     * @param  {Boolean} isToken  是否携带token
     * @return {Promise}       包含抓取任务的Promise
     */
    getApi(url, params, isToken, isbinding) {
        if (isToken === undefined) isToken = true;
        if (isbinding === undefined) isbinding = true;
        let token = wx.getStorageSync('token') || '';
        const promise = new Promise((resolve, reject) => {
            //判断是否需要置入token
          if (isToken) params = Object.assign({sessionId:token}, params)
            wx.request({
                url: `${BASE_URL}${url}`,
                method: 'POST',
                data: params, //置入token
                header: { 'Content-Type': 'application/x-www-form-urlencoded'},
                success: res => {
                    console.log('getApi--res==', res);
                    
                    if (res.statusCode == 200) {
                      if (res.data.code==1){
                        resolve(res);
                      } else if (res.data.code == 2) {
                        getApp().ToLogin('是否授权登录')
                        reject();
                      } else if (res.data.code == 3) {
                        getApp().ToLogin('需绑定手机号')
                        if(isbinding){
                          reject();
                        }else{
                          resolve(res);
                        }
                      }else{
                        wx.showToast({
                          title: res.data.desc,
                          icon: 'none',
                          duration: 800
                        });
                        reject();
                      }
                      
                    } else {
                        wx.showToast({
                            title: '[' + res.code + '] 服务器出错,请重试',
                            icon: 'none',
                            duration:800
                        });
                        reject();
                    }
                },
                fail: err => {
                    console.log('fail--err==', err);
                    
                    wx.showToast({
                        title: '网络错误',
                        icon: 'none',
                        duration:800
                    });
                    reject();
                },
                complete: res => {

                }
            });
        });

        if (token == '' && isToken) {
          return new Promise((resolve, reject) => {
              // wx.showToast({
              //     title: '状态失效，请关闭小程序后，重新打开',
              //     icon: 'none'
              // });
          });
        } else {
            return promise;
        }
    }

}

let request = new Request();

module.exports = {
    get: request.getApi,
    post: request.getApi,
    url: BASE_URL
}